# 封装发送短信的函数
from celery_tasks.main import celery_app
from celery_tasks.sms.yuntongxun.sms import CCP

# 获取日志器
import logging

logger = logging.getLogger('django')

# 获取短信模板id
SMS_CODE_TEMP_ID = 1


@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code, expires):
    # 3.使用云通讯给mobile发送短信
    try:
        res = CCP().send_template_sms(mobile, [sms_code, expires], SMS_CODE_TEMP_ID)
    except Exception as e:
        logger.error('发送短信异常: mobile: %s sms_code: %s' % (mobile, sms_code))
    else:  # 没有出现异常时会执行else下面的代码
        if res != 0:
            logger.error('发送短信失败: mobile: %s sms_code: %s' % (mobile, sms_code))
        else:  # 发送短信成功  logger.info 是在打印日志
            logger.info('发送短信成功: mobile: %s sms_code: %s' % (mobile, sms_code))
